package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
 @Autowired
 private SetmealService setmealService;

 @PostMapping
 @CacheEvict(value = "setmeal", allEntries = true)
 public R<String> save(@RequestBody SetmealDto setmealDto) {
  log.info("setmealDto: {}", setmealDto);
  R<String> stringR = setmealService.save1(setmealDto);
  return stringR;
 }

 @GetMapping("/page")
 public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
  log.info("page :{}", page);
  Page<SetmealDto> pageInfo = setmealService.page(page, pageSize, name);
  return R.success(pageInfo);
 }

 @DeleteMapping
 @CacheEvict(value = "setmeal", allEntries = true)
 public R<String> delete(@RequestParam("ids") List<Long> ids) {
  log.info("ids:{}", ids);
  R<String> stringR = setmealService.deleteByIds(ids);
  return stringR;
 }

 @PostMapping("/status/{status}")
 @CacheEvict(value = "setmeal", allEntries = true)
 public R<String> updateStatus(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {
  log.info("ids:{}", ids);
  R<String> stringR = setmealService.updateStatus(status, ids);
  return stringR;
 }

 @GetMapping("/list")
 @Cacheable(value = "setmeal", key = "#setmeal.categoryId+'-'+#setmeal.status")
 public R<List<Setmeal>> getDish(Setmeal setmeal) {
  R<List<Setmeal>> dishs = setmealService.getDish(setmeal);
  return dishs;
 }

 @GetMapping("/{id}")
 public R<Setmeal> getSetmealById(@PathVariable Long id) {
  log.info("套餐信息");
  Setmeal setmeal = setmealService.getByIdWithDishes(id);
  return R.success(setmeal);
 }

 @PutMapping
 public R<Setmeal> upDate(@RequestBody SetmealDto setmealDto) {
  log.info("修改套餐信息");
  SetmealDto upDateWithSetmealDishes = setmealService.upDateWithSetmealDishes(setmealDto);
  return R.success(upDateWithSetmealDishes);
 }

}
